//
//  WBHomeViewController.swift
//  新浪微博
//
//  Created by apple on 2016/11/29.
//  Copyright © 2016年 yanjun. All rights reserved.
//

import UIKit

// 定义全局常量，尽量使用 private 修饰，否则导出都可以访问
private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

    
    // 列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }
    
   
    
    // 记载数据
    override func loadData() {
        
        
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess,shouldfRefresh) in
            
            
            self.refreshControl?.endRefreshing()
            
            // 恢复上拉刷新标记
            self.isPullup = false
            
            if shouldfRefresh {
                
                self.tableView?.reloadData()
            }
            
        }
        
        
        WBNetworkManager.shared().statusList { (list, isSuccess) in
            
            print(list!)
            
            
        }
        
        
        
        
    }
    
    // 重写父类方法
    override func setupTableView() {
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)

        setupNavTitle()
    }

    // 设置导航栏标题
    fileprivate func setupNavTitle() {
        
        let title = WBNetworkManager.shared().userAccount.screen_name
        
        let button = WBTitleButton(title: title)
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
        
        navItem.titleView = button
        
        
    }
    
    @objc func clickTitleButton(btn: UIButton) {
        
        btn.isSelected = !btn.isSelected
        
    }
    
    
    func showFriends() {
        print(#function)
        
        let vc = DemoViewController()
        navigationController?.pushViewController(vc, animated: true)
        
    }

    
}

// MARK: - 表格数据源方法,具体的数据源方法实现，不需要 super
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        return cell
    }
    
}

